package pkg.andru.core;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

public class CommonFragmentActivity extends FragmentActivity {

    protected CommonFragmentManager mCommonFragmentManager;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mCommonFragmentManager = new CommonFragmentManager(this);
    }

    public CommonFragmentManager getCommonFragmentManager() {
        return mCommonFragmentManager;
    }

    @Override
    protected void onDestroy() {
        if (mCommonFragmentManager != null) {
            mCommonFragmentManager.release();
        }
        super.onDestroy();
    }

    public void processOnClick(View view) {
        final CommonFragment fragment = mCommonFragmentManager.getCurrentFragment();
        if (fragment != null) {
            fragment.processOnClick(view);
        }
    }

    /**
     * override the activity's onback pressed, the default implements is pop the
     * fragment in the back stack, when the back stack has only one, will
     * directed to finsh the activity
     */
    @Override
    public void onBackPressed() {
        if (!mCommonFragmentManager.popBackStackFragment()) {
            finish();
        }
    }

}
